package cn.wolfcode.car.business.web.contorller;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.http.HttpResponse;

/**
 * 养修服务控制器
 */
@Controller
@RequestMapping("business/bpmnInfo")
public class BpmnInfoController {
    //模板前缀
    private static final String prefix = "business/system/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;
    @Autowired
    private IStatementService statementService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("business:bpmnInfo:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }


    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo) {
        return bpmnInfoService.query(qo);
    }


    //新增
    @RequiresPermissions("business:bpmnInfo:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BpmnInfo bpmnInfo) {
        bpmnInfoService.save(bpmnInfo);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:bpmnInfo:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(String info ,Long id) {
        bpmnInfoService.updateById(info,id);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:bpmnInfo:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        bpmnInfoService.deleteBatch(ids);
        return AjaxResult.success();
    }

    @RequiresPermissions("business:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage() {
        // model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "deploy";
    }

    //文件上传upload
    @RequiresPermissions("business:bpmnInfo:upload")
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) {

        if (file != null && file.getSize() > 0) {

            String filename = file.getOriginalFilename(); //获取文件的名称
            String exp = FilenameUtils.getExtension(filename);//获取文件名的后缀
            if ("zip".equalsIgnoreCase(exp) || "bpmn".equalsIgnoreCase(exp)) { //忽略大小写比较 如果后缀名是zip或bpmn结尾 是需要的文件
                String upload = null;
                try {        //FileUploadUtil工具类的upload 保存文件到指定路径下
                    upload =   FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
                } catch (IOException e) {
                    e.printStackTrace();
                    return AjaxResult.error("流程文件上传失败");
                }
                return AjaxResult.success("上传成功",upload);
            } else {
                return AjaxResult.error("流程定义文件仅支持bpmn格式和zip格式");
            }
        }else{
            return AjaxResult.error("不允许传空文件");
        }

    }
    //部署
    @RequiresPermissions("business:bpmnInfo:deploy")
    @RequestMapping("/deploy")
    @ResponseBody
    public AjaxResult  deploy(String bpmnPath , String bpmnType , String info) throws IOException {
        bpmnInfoService.deploy(bpmnPath , bpmnType , info);

        return AjaxResult.success();

    }
   //查看流程文件
 /*   @RequiresPermissions("business:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    @ResponseBody
    public AjaxResult readResource(String ids) {
        bpmnInfoService.deleteBatch(ids);
        return AjaxResult.success();
    }*/
   //流程删除

    @RequiresPermissions("business:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id) {

       bpmnInfoService.delete(id);

        return AjaxResult.success();
    }

    //查看流程图
    @RequiresPermissions("business:bpmnInfo:readResource")
    @RequestMapping("/readResource")
    @ResponseBody
    public AjaxResult  readResource(String deploymentId, String type , HttpServletResponse response) throws IOException {

        InputStream inputStream = bpmnInfoService.getResoucceByType(deploymentId ,type);
        IOUtils.copy(inputStream,response.getOutputStream());

        return AjaxResult.success();

    }

}



